﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Reflection;
using Microsoft.AspNetCore.StaticFiles;
using Newtonsoft.Json;
using System.Collections.Generic;
using Microsoft.AspNetCore.Hosting;
using System.Text;
using Microsoft.Extensions.Hosting;

namespace WucCloud.Utility
{
    public class GlobalContext
    {
        /// <summary>
        /// All registered service and class instance container. Which are used for dependency injection.
        /// </summary>
        public static IServiceCollection Services { get; set; }

        /// <summary>
        /// Configured service provider.
        /// </summary>
        public static IServiceProvider ServiceProvider { get; set; }

        public static IConfiguration Configuration { get; set; }

        public static IWebHostEnvironment HostingEnvironment { get; set; }

        public static AppSettingConfig AppSettingConfig { get; set; }

        /// <summary>
        /// 获取版本号
        /// </summary>
        /// <returns></returns>
        public static string GetVersion()
        {
            Version version = Assembly.GetEntryAssembly().GetName().Version;
            return version.ToString();
        }

        /// <summary>
        /// 程序启动时，记录目录
        /// </summary>
        /// <param name = "env" ></ param >
        public static void LogWhenStart(IWebHostEnvironment env)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("程序启动");
            sb.AppendLine("ContentRootPath:" + env.ContentRootPath);
            sb.AppendLine("WebRootPath:" + env.WebRootPath);
            sb.AppendLine("IsDevelopment:" + env.IsDevelopment());
            //LogHelper.WriteWithTime(sb.ToString());
        }

        /// <summary>
        /// 设置cache control
        /// </summary>
        /// <param name="context"></param>
        public static void SetCacheControl(StaticFileResponseContext context)
        {
            int second = 365 * 24 * 60 * 60;
            context.Context.Response.Headers.Add("Cache-Control", new[] { "public,max-age=" + second });
            context.Context.Response.Headers.Add("Expires", new[] { DateTime.UtcNow.AddYears(1).ToString("R") }); // Format RFC1123
        }

        /// <summary>
        /// 写入session
        /// </summary>
        /// <param name="entity">用户登录信息</param>
        public static void SetCurrentUserInfo(OperatorUser entity)
        {
            HttpContextHelper.SetSession(Define.CurrentUserInfo, entity);
        }

        /// <summary>
        /// 设置当前登录用户的角色
        /// </summary>
        /// <param name="list"></param>
        public static void SetCurrentUserRole(List<string> list)
        {
            HttpContextHelper.SetSession(Define.CurrentUserRoles, list);
        }

        /// <summary>
        /// 设置当前登录用户的权限节点
        /// </summary>
        /// <param name="list"></param>
        //public static void SetCurrentAuthNode(List<SysNode> list)
        //{
        //    HttpContextExtensions.SetSession(Define.CurrentUserAuthNode, list);
        //}

        /// <summary>
        /// 获取当前登录用户信息
        /// </summary>
        /// <returns></returns>
        public static OperatorUser CurrentUser()
        {
            var userStr = HttpContextHelper.GetSession(Define.CurrentUserInfo);
            if (!string.IsNullOrEmpty(userStr))
            {
                return JsonConvert.DeserializeObject<OperatorUser>(userStr);
            }
            return null;
        }

        /// <summary>
        /// 获取当前登录用户的权限节点
        /// </summary>
        /// <returns></returns>
        //public static List<SysNode> CurrentAuthNode()
        //{
        //    var userStr = HttpContextExtensions.GetSession(Define.CurrentUserAuthNode);
        //    if (!string.IsNullOrEmpty(userStr))
        //    {
        //        return JsonConvert.DeserializeObject<List<SysNode>>(userStr);
        //    }
        //    return null;
        //}

        /// <summary>
        /// 获取当前登录用户的角色ID
        /// </summary>
        /// <returns></returns>
        public static List<string> CurrentUserRole()
        {
            var userStr = HttpContextHelper.GetSession(Define.CurrentUserRoles);
            if (!string.IsNullOrEmpty(userStr))
            {
                return JsonConvert.DeserializeObject<List<string>>(userStr);
            }
            return null;
        }

        /// <summary>
        /// 获取当前验证码
        /// </summary>
        /// <returns></returns>
        public static string CurrentVerify()
        {
            return HttpContextHelper.GetSession(Define.VerifyCode);
        }

        /// <summary>
        /// 设置当前验证码
        /// </summary>
        /// <param name="code"></param>
        public static void SetVerify(string code)
        {
            HttpContextHelper.SetSession(Define.VerifyCode, code);
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        public static void SignOut()
        {
            HttpContextHelper.SetSession(Define.CurrentUserInfo, string.Empty);
            HttpContextHelper.SetSession(Define.CurrentUserAuthNode, string.Empty);
        }

        #region WeChat
        /// <summary>
        /// 设置微信当前登录用户唯一标识
        /// </summary>
        /// <param name="openId"></param>
        public static void SetOpenId(string openId)
        {
            HttpContextHelper.SetSession(Define.OPENID, openId);
        }

        /// <summary>
        /// 设置微信会话密钥
        /// </summary>
        /// <param name="openId"></param>
        public static void SetSessionKey(string sessionKey)
        {
            HttpContextHelper.SetSession(Define.SESSIONKEY, sessionKey);
        }
        #endregion
    }
}
